<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mydb_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
    function add($table,$data){
		$this->db->insert($table, $data);
		return $this->db->affected_rows();
    }
    
    function update($table,$data,$where){
    	if(!empty($where)){
    		$this->db->where($where);
    	}
		$this->db->update($table, $data);
		return $this->db->affected_rows();
    }
    
    function get_one($table,$where = array(),$select='*'){
		$this->db->select($select);
		if(!empty($where)){
			$this->db->where($where);
		}
		$this->db->limit(1);
		$query = $this->db->get($table);
		return $query->row_array();
	}
	
	function get_list($table,$where = array(),$select = '*')
	{		
		$this->db->select($select);
		if(!empty($where)){			
			$this->db->where($where);
		}				
		$query = $this->db->get($table);		
		return $query->result_array();
	}
	
	function delete($table, $where)
	{
		$this->db->delete($table, $where);
		return $this->db->affected_rows();
	}
	
	function query_sql($sql,$act)
	{
		if(!in_array($act,array('row','list','set','del','add'))){
			return false;
		}else{
			$query = $this->db->query($sql);
			if($act == 'set' || $act == 'del')
			{			
				return $this->db->affected_rows(); 
			}elseif($act == 'row'){
				return $query->row_array();
			}elseif($act == 'list'){
				return $query->result_array();
			}elseif($act == 'add'){
				return $this->db->insert_id();
			}
		}
	}
}